from fastapi import FastAPI
from pydantic import BaseModel
import subprocess

app = FastAPI()
workdir = "/app/"

class BatFileRequest(BaseModel):
    # 假设bat文件的名字固定为my_spider.bat，且位于容器的工作目录下
    bat_file_name: str = "zyyszcryautoCheck.bat"


@app.post("/run_bat/")
async def run_bat_file(request: BatFileRequest):
    bat_file_path = request.bat_file_name

    try:
        # 直接使用相对路径执行批处理文件
        subprocess.Popen(bat_file_path, shell=True, check=True)
        return {"status": "success", "message": "BAT文件执行成功"}
    except subprocess.CalledProcessError as e:
        return {"status": "error", "message": f"执行BAT文件时出错: {e}"}
    except FileNotFoundError:
        return {"status": "error", "message": "指定的BAT文件不存在"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)
